<template>
  <div class="holiday-page">
    <el-card>
      <template #header>
        <div class="card-header">
          <span class="title">🎉 节日策略管理</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>
            新增策略
          </el-button>
        </div>
      </template>

      <el-table :data="tableData" border stripe>
        <el-table-column prop="name" label="节日名称" width="150" />
        <el-table-column prop="date" label="日期" width="150" />
        <el-table-column prop="strategyType" label="策略类型" width="150">
          <template #default="{ row }">
            {{ getStrategyTypeName(row.strategyType) }}
          </template>
        </el-table-column>
        <el-table-column prop="value" label="优惠内容" width="250">
          <template #default="{ row }">
            {{ getStrategyValue(row) }}
          </template>
        </el-table-column>
        <el-table-column prop="targetLevel" label="适用对象" width="200">
          <template #default="{ row }">
            <el-tag v-for="l in row.targetLevel" :key="l" size="small" style="margin-right: 5px;">
              {{ l === 'all' ? '全部会员' : getLevelName(l) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="enabled" label="启用" width="80">
          <template #default="{ row }">
            <el-switch v-model="row.enabled" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button link type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="700px">
      <el-form :model="form" label-width="100px">
        <el-form-item label="节日名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="节日日期">
          <el-date-picker v-model="form.date" type="date" value-format="MM-DD" />
        </el-form-item>
        <el-form-item label="策略类型">
          <el-select v-model="form.strategyType">
            <el-option label="赠送积分" value="points" />
            <el-option label="赠送礼券" value="coupon" />
            <el-option label="赠送礼金" value="cash" />
            <el-option label="折扣优惠" value="discount" />
          </el-select>
        </el-form-item>
        <el-form-item :label="getValueLabel()">
          <el-input-number v-model="form.value" :min="0" />
        </el-form-item>
        <el-form-item label="适用对象">
          <el-checkbox-group v-model="form.targetLevel">
            <el-checkbox label="all">全部会员</el-checkbox>
            <el-checkbox label="silver">银卡</el-checkbox>
            <el-checkbox label="gold">金卡</el-checkbox>
            <el-checkbox label="diamond">钻石卡</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="启用状态">
          <el-switch v-model="form.enabled" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

const activeTab = ref('levels')
const dialogVisible = ref(false)
const dialogTitle = ref('新增策略')

const tableData = ref([
  { name: '春节', date: '01-01', strategyType: 'points', value: 500, targetLevel: ['all'], enabled: true },
  { name: '会员生日', date: '', strategyType: 'cash', value: 100, targetLevel: ['gold', 'diamond'], enabled: true },
  { name: '国庆节', date: '10-01', strategyType: 'coupon', value: 50, targetLevel: ['all'], enabled: true },
  { name: '双十一', date: '11-11', strategyType: 'discount', value: 20, targetLevel: ['all'], enabled: true }
])

const form = reactive({
  name: '',
  date: '',
  strategyType: 'points',
  value: 0,
  targetLevel: ['all'],
  enabled: true
})

const getStrategyTypeName = (type) => {
  const map = { points: '赠送积分', coupon: '赠送礼券', cash: '赠送礼金', discount: '折扣优惠' }
  return map[type] || type
}

const getStrategyValue = (row) => {
  if (row.strategyType === 'points') return `赠送${row.value}积分`
  if (row.strategyType === 'coupon') return `赠送¥${row.value}礼券`
  if (row.strategyType === 'cash') return `赠送¥${row.value}礼金`
  if (row.strategyType === 'discount') return `额外${row.value}%折扣`
  return '-'
}

const getLevelName = (level) => {
  const map = { silver: '银卡', gold: '金卡', diamond: '钻石卡' }
  return map[level] || level
}

const getValueLabel = () => {
  const map = { points: '积分数量', coupon: '礼券面值', cash: '礼金金额', discount: '折扣百分比' }
  return map[form.strategyType] || '值'
}

const handleAddLevel = () => {
  ElMessage.info('新增等级')
}

const handleAdd = () => {
  dialogTitle.value = '新增策略'
  dialogVisible.value = true
}

const handleEdit = (row) => {
  dialogTitle.value = '编辑策略'
  Object.assign(form, row)
  dialogVisible.value = true
}

const handleDelete = () => {
  ElMessage.success('删除成功')
}

const handleSubmit = () => {
  ElMessage.success('保存成功')
  dialogVisible.value = false
}
</script>

<style scoped>
.holiday-page {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}
</style>

